﻿Option Strict On
Option Explicit On
Imports System.Convert
Imports System.Random
Imports System.Text.RegularExpressions
Imports System.Net


Module CommonPublicFunctions
    Public SharedVisitId As String
    Public GeneralImageSet As ImageList
    Public NavigationImageSet As ImageList
    Public StatisticsImageSet As ImageList

    Public Function InitImageLists() As Boolean
        GeneralImageSet = New ImageList()
        GeneralImageSet.ImageSize = New Size(70, 70)
        GeneralImageSet.TransparentColor = Color.Transparent
        GeneralImageSet.ColorDepth = ColorDepth.Depth32Bit
        GeneralImageSet.Images.Add("Add", My.Resources.Add)
        GeneralImageSet.Images.Add("Save", My.Resources.Save)
        GeneralImageSet.Images.Add("Back", My.Resources.Back)
        GeneralImageSet.Images.Add("Search", My.Resources.Search)
        GeneralImageSet.Images.Add("AddNew", My.Resources.AddNew)
        GeneralImageSet.Images.Add("Delete", My.Resources.Delete)
        GeneralImageSet.Images.Add("Info", My.Resources.Info)
        GeneralImageSet.Images.Add("DeleteItem", My.Resources.RecyclebinEmpty)

        NavigationImageSet = New ImageList()
        NavigationImageSet.ImageSize = New Size(60, 40)
        NavigationImageSet.TransparentColor = Color.Transparent
        NavigationImageSet.ColorDepth = ColorDepth.Depth32Bit
        NavigationImageSet.Images.Add("Back", My.Resources.Back)
        NavigationImageSet.Images.Add("Forward", My.Resources.Forward)
        NavigationImageSet.Images.Add("First", My.Resources.First)
        NavigationImageSet.Images.Add("Last", My.Resources.Last)
        NavigationImageSet.Images.Add("BackSelected", My.Resources.BackSelected)
        NavigationImageSet.Images.Add("ForwardSelected", My.Resources.ForwardSelected)
        NavigationImageSet.Images.Add("FirstSelected", My.Resources.FirstSelected)
        NavigationImageSet.Images.Add("LastSelected", My.Resources.LastSelected)

        StatisticsImageSet = New ImageList()
        StatisticsImageSet.ImageSize = New Size(45, 25)
        StatisticsImageSet.TransparentColor = Color.Transparent
        StatisticsImageSet.ColorDepth = ColorDepth.Depth32Bit
        StatisticsImageSet.Images.Add("Go", My.Resources.Go)
        StatisticsImageSet.Images.Add("GoSelected", My.Resources.GoSelected)
    End Function

    Public Function SwitchForm(ByRef Form As Windows.Forms.Form) As Boolean
        ' Lets hide em'

        Try
            frmMain.Visible = False
            frmSurvey.Visible = False
            frmStatistics.Visible = False
            frmViewEmployee.Visible = False
            frmDeleteEmployee.Visible = False
            frmAddEmployee.Visible = False
            frmViewInventory.Visible = False
            frmDeleteInventory.Visible = False
            frmAddInventory.Visible = False
            frmQuickVisit.Visible = False
            frmDetailedVisit.Visible = False

            ' If it was created it will just be set to visible, otherwise
            ' Let's bring it up for the first time.

            If Form.IsHandleCreated Then
                Form.Visible = True
            Else
                Form.ShowDialog()
            End If

            Return True

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try

    End Function

    Public Function GenerateRandomNumber(ByVal Min As Integer, _
                                      ByVal Max As Integer) As Integer

        Dim InitialSeed As New System.Random(My.Computer.Clock.TickCount)

        Dim Seed As Integer = ToInt32((My.Computer.Clock.TickCount / _
                                     ((My.Computer.Info.AvailablePhysicalMemory / 1024) + 1)) + _
                                      (My.Computer.Clock.LocalTime.Millisecond / _
                                      (My.Computer.Clock.LocalTime.Minute + 1)) + _
                                       My.Computer.Mouse.WheelScrollLines + _
                                       My.Computer.Clock.LocalTime.Month * _
                                       My.Computer.Clock.LocalTime.Day + _
                                       My.Computer.Clock.LocalTime.Hour + _
                                      (My.Computer.Clock.TickCount / _
                                       My.Computer.Clock.LocalTime.Millisecond) - _
                                       InitialSeed.Next(Min, ToInt32(Max)))

        Dim RND As New System.Random(Seed)

        Return RND.Next(Min, Max + 1)
    End Function

    Public Function CloseAllForms() As Boolean
        Try
            frmMain.Close()
            frmViewInventory.Close()
            frmMain.Close()
            frmSurvey.Close()
            Return True
        Catch ex As Exception
            MessageBox.Show("Something weird happened when I tried close all forms", _
                            "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try

    End Function

    Public Function RemoveExtraWhiteSpaces(ByVal strIn As String) As String
        Return CStr(IIf(strIn.Trim(CChar(" ")).Length = 0, strIn, Regex.Replace(strIn, " {2,}", " ")))
    End Function

    Public Function GetIPAddress() As String
        Dim theIPAddress As String = "127.0.0.1"
        Dim ipEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
        Dim addresses As IPAddress() = ipEntry.AddressList

        If addresses.Count > 0 Then
            theIPAddress = addresses(0).ToString()
        End If

        Return theIPAddress
    End Function

End Module
